package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.DanceKinds;
import com.example.demo.entity.Notice;
import com.example.demo.entity.Rotation;
import com.example.demo.mapper.NoticeMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    NoticeMapper noticeMapper;


    @PostMapping
    public Result<?> save(@RequestBody Notice notice) {

        noticeMapper.insert(notice);
        return Result.success();

    }

    @PutMapping
    public Result<?> update(@RequestBody Notice notice) {
        noticeMapper.updateById(notice);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        noticeMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");

        LambdaQueryWrapper<Notice> wrapper = Wrappers.<Notice>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Notice::getNotyTittle, search);
        }
        Page<Notice> stuPage = noticeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(stuPage);

    }

    @GetMapping("/wx")
    public Result<?> notice() {

        List<Notice> noticeList = noticeMapper.selectList(null);
        return Result.success(noticeList);

    }
    @PostMapping("/detail")
    public Notice detail(@RequestBody JSONObject req) {
        String id = req.getStr("id");
        Notice notice =  noticeMapper.selectById(id);
        return notice;


    }
}
